package com.douding.business.controller.admin;

import com.douding.server.domain.Course;
import com.douding.server.domain.CourseContent;
import com.douding.server.dto.*;
import com.douding.server.service.CourseCategoryService;
import com.douding.server.service.CourseService;
import com.douding.server.util.UuidUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/admin/course")
public class CourseController {

    private static final Logger LOG = LoggerFactory.getLogger(CourseController.class);

    public static final String BUSINESS_NAME = "课程";

    @Resource
    private CourseService courseService;

    @Resource
    private CourseCategoryService courseCategoryService;

    @PostMapping("/list-category/{courseId}")
    public ResponseDto listCategory(@PathVariable("courseId") String courseId) {
        List<CourseCategoryDto> courseCategoryDtoList = courseCategoryService.listByCourse(courseId);
        ResponseDto responseDto = new ResponseDto();
        responseDto.setContent(courseCategoryDtoList);
        return responseDto;
    }

    @RequestMapping("/list")
    public ResponseDto list(CoursePageDto pageDto) {
        ResponseDto responseDto = new ResponseDto();
        courseService.selectByPage(pageDto);
        responseDto.setContent(pageDto);
        return responseDto;
    }

    @PostMapping("/save")
    public ResponseDto save(@RequestBody CourseDto courseDto) {
        courseService.save(courseDto);
        boolean flag = courseDto.getId() == null;
        return flag ? ResponseDto.saveSuccess() : ResponseDto.updateSuccess();
    }

    @DeleteMapping("/delete/{id}")
    public ResponseDto delete(@PathVariable String id) {
        courseService.deleteById(id);
        return ResponseDto.success();
    }

    // 课程内容相关查找
    @GetMapping("/find-content/{id}")
    public ResponseDto findContent(@PathVariable String id) {
        CourseContent courseContent = courseService.findContent(id);
        ResponseDto responseDto = new ResponseDto();
        responseDto.setContent(courseContent);
        return responseDto;
    }

    // 课程内容保存
    @PostMapping("/save-content")
    public ResponseDto saveContent(@RequestBody CourseContentDto courseContentDto) {
        courseService.saveContent(courseContentDto);
        boolean flag = courseContentDto.getId() == null;
        return flag ? ResponseDto.saveSuccess() : ResponseDto.updateSuccess();
    }

    // 课程排序
    @PostMapping("/sort")
    public ResponseDto sort(@RequestBody SortDto sortDto) {
        courseService.sort(sortDto);
        return ResponseDto.success();
    }

}
